﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp
{
    public class MyScheduler : TaskScheduler
    {
        protected override void QueueTask(Task task)
        {
            // 将任务排队到线程池中
            ThreadPool.QueueUserWorkItem(_ => TryExecuteTask(task));
        }

        protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
        {
            // 如果当前线程是调度器的线程，则在当前线程内执行任务
            if (taskWasPreviouslyQueued && Thread.CurrentThread.IsThreadPoolThread)
                return TryExecuteTask(task);

            return false;
        }

        protected override IEnumerable<Task> GetScheduledTasks()
        {
            // 此方法用于调度器的内部追踪，不要求实现
            return Enumerable.Empty<Task>();
        }
    }
}
